﻿Public Class MASAGENT
    Private AGENTNAME As String
    Private AGENTLASTNAME As String
    Private AGENTADD As String
    Private AGENTEMAIL As String
    Private AGENTTELL As String
    Private AGENTAMPUR As Integer
    Private AGENTCHANGWAT As Integer
    Private AGENTID As Integer = Nothing
    Private SELECTMASAGENT = Nothing
    Dim connect As CONDB = CONDB.NewConnection
    Property SELECTMASAGENT_() As String
        Get
            SELECTMASAGENT = "SELECT `agentid` as 'รหัส',`agentname` as 'ชื่อตัวแทน',`agentlastname` as 'นามสกุล',`agentadd` as 'ที่อยู่',`agenttell` as 'เบอร์โทรศัพท์',masagent.`agentampur`,ampur.`ampur` as 'อำเภอ',masagent.`agentchangwat`,changwat.`changwat` as 'จังหวัด',masagent.`status` as 'สถานะการเปิดใช้งาน'  "
            SELECTMASAGENT += "FROM cpm.masagent "
            SELECTMASAGENT += "LEFT JOIN (SELECT `idmaschangwat`,`changwat` FROM cpm.maschangwat WHERE `status` = 1) as changwat ON masagent.`agentchangwat` = changwat.`idmaschangwat` "
            SELECTMASAGENT += "LEFT JOIN (SELECT `idmasampur`,`ampur` FROM cpm.masampur WHERE `status` = 1) as ampur ON masagent.`agentampur` = ampur.`idmasampur` "
            Return SELECTMASAGENT
        End Get
        Set(value As String)

        End Set
    End Property

    Property AGENTID_() As String
        Get
            Return AGENTID
        End Get
        Set(value As String)
            AGENTID = value
        End Set
    End Property
    Property AGENTNAME_() As String
        Get
            Return AGENTNAME
        End Get
        Set(value As String)
            AGENTNAME = value
        End Set
    End Property
    Property AGENTLASTNAME_() As String
        Get
            Return AGENTLASTNAME
        End Get
        Set(value As String)
            AGENTLASTNAME = value
        End Set
    End Property
    Property AGENTADD_() As String
        Get
            Return AGENTADD
        End Get
        Set(value As String)
            AGENTADD = value
        End Set
    End Property
    Property AGENTTELL_() As String
        Get
            Return AGENTTELL
        End Get
        Set(value As String)
            AGENTTELL = value
        End Set
    End Property
    Property AGENTTEL_() As String
        Get
            Return AGENTTELL
        End Get
        Set(value As String)
            AGENTTELL = value
        End Set
    End Property

    Property AGENTAMPUR_() As String
        Get
            Return AGENTAMPUR
        End Get
        Set(value As String)
            AGENTAMPUR = value
        End Set
    End Property

    Property AGENTCHANGWAT_() As String
        Get
            Return AGENTCHANGWAT
        End Get
        Set(value As String)
            AGENTCHANGWAT = value
        End Set
    End Property

    Public Sub INSERTMASAGENT()
        connect = CONDB.NewConnection()
        connect.BeginTrans()
        Dim sql As String
        Try
            sql = "INSERT INTO cpm.masagent"
            sql += "(AGENTNAME,AGENTLASTNAME,AGENTADD,AGENTTELL,AGENTAMPUR,AGENTCHANGWAT) "
            sql += " VALUES ("
            sql += " '" & AGENTNAME_ & "' , "
            sql += " '" & AGENTLASTNAME_ & "' , "
            sql += " '" & AGENTADD_ & "' , "
            sql += " '" & AGENTTEL_ & "' , "
            sql += " '" & AGENTAMPUR_ & "' , "
            sql += " '" & AGENTCHANGWAT_ & "' ); "
            connect.ExecuteNonQuery(sql)
            connect.CommitTrans()
            MsgBox("บันทึกข้อมูลเรียบร้อย", MsgBoxStyle.Information)
        Catch ex As Exception
            connect.RollbackTrans()
            MsgBox(ex.ToString)
        Finally
            connect.Dispose()
        End Try

    End Sub
    Public Sub UPDATEMASAGENT()
        connect = CONDB.NewConnection()
        connect.BeginTrans()
        Dim sql As String
        Try
            sql = "UPDATE cpm.masagent SET "
            sql += " AGENTNAME = '" & AGENTNAME_ & "' , "
            sql += " AGENTLASTNAME = '" & AGENTLASTNAME_ & "' , "
            sql += " AGENTADD = '" & AGENTADD_ & "' , "
            sql += " AGENTTELL = '" & AGENTTEL_ & "' , "
            sql += " AGENTAMPUR = '" & AGENTAMPUR_ & "' , "
            sql += " AGENTCHANGWAT = '" & AGENTCHANGWAT_ & "'  "
            sql += " WHERE  AGENTID ='" & AGENTID_ & "'; "
            connect.ExecuteNonQuery(sql)
            connect.CommitTrans()
        Catch ex As Exception
            connect.RollbackTrans()
            MsgBox(ex.ToString)
        Finally
            connect.Dispose()
        End Try
      
    End Sub

    'Public Function LOADMASAGENT() As DataTable
    '    Dim dt As DataTable()

    '    Dim sql As String
    '    sql = "SELECT * FROM masagent LEFT JOIN masampur ON masagent.AGENTAMPUR =  masampur.idmasampur "

    '    Return dt
    'End Function
End Class
